/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.editor;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

/**
 * @author smart
 * 
 */
public class App {

    public static final int SINGLETON_TYPE = 0;
    public static final int TABDED_TYPE = 1;
    private static int type = TABDED_TYPE;

    public static int getType() {
        return type;
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        new App(new TabbedEditViewFactory()).f1();
    }
    private EditViewFactory vf;
    private int iteration = 1;

    public EditViewFactory getVf() {
        return vf;
    }

    public void setVf(EditViewFactory vf) {
        this.vf = vf;
    }

    public App(EditViewFactory vf) {
        this.vf = vf;
    }

    public void f1() {
        final AbstractEditViewFactory avf = new AbstractEditViewFactory();
        final JTextField tf = new JTextField();
        tf.setEditable(false);

        //TODO: not i18n;
        JComboBox cb = new JComboBox(new Type[]{new Type(SINGLETON_TYPE, "singleton"), new Type(TABDED_TYPE, "tabbed")});
        cb.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                Type source = (Type) ((JComboBox) arg0.getSource()).getSelectedItem();
                type = source.getVal();
                tf.setText(source.toString());
                App.this.setVf(avf.createEditViewFactory());
            }
        });
        cb.setSelectedIndex(0);

        JButton btn = new JButton(new AbstractAction("View") {

            @Override
            public void actionPerformed(ActionEvent e) {
                vf.createView("Some text" + iteration, "titlle " + iteration++, null);
            }
        });

        JFrame fr = new JFrame("Text editor");// TODO: not i18n
        fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        fr.getContentPane().add(cb, BorderLayout.NORTH);
        fr.getContentPane().add(tf, BorderLayout.CENTER);
        fr.getContentPane().add(btn, BorderLayout.EAST);
        fr.getContentPane().add(new JButton(new AbstractAction("exit") {

            @Override
            public void actionPerformed(ActionEvent arg0) {
                System.exit(0);
            }
        }), BorderLayout.SOUTH);
        fr.setLocationRelativeTo(null);
        fr.pack();
        fr.setVisible(true);
    }

    static class Type {

        int val;
        String text;

        public Type(int val, String text) {
            super();
            this.val = val;
            this.text = text;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        @Override
        public String toString() {
            return text;
        }
    }
}
